package com.appcenter.application.service;

import com.appcenter.application.input.App.*;
import com.appcenter.application.mapper.AppsMapper;
import com.appcenter.application.model.Apps;
import com.appcenter.application.output.App.*;
import com.appcenter.jz.config.ErrorJsonResult;
import com.appcenter.jz.page.PagedResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

@Service
public class AppService {
    @Autowired
    AppsMapper AppMapper;

    private final Logger logger = LoggerFactory.getLogger(this.getClass());


    @Transactional
    public CreateAppOutput CreateApp(CreateAppInput input){

        logger.error("123123123");

        CreateAppOutput output = new CreateAppOutput();
        int AppCodeCount=AppMapper.GetAllAppsByAppCode(input.getItem().getAppCode()).size();
        if (AppCodeCount > 0)
            throw new ErrorJsonResult("1000031","应用编号已存在！");
        int AppNameCount=AppMapper.GetAllAppsByAppName(input.getItem().getAppName()).size();

        if (AppNameCount > 0)
            throw new ErrorJsonResult("1000031","应用名称已存在！");

        String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase();
        Apps app = new Apps();
        app.setId(uuid);
        app.setAppCode(input.getItem().getAppCode());
        app.setAppCode(input.getItem().getAppCode());
        app.setAppName(input.getItem().getAppName());
        app.setDescription(input.getItem().getDescription());
        app.setHomeUrl(input.getItem().getHomeUrl());
        app.setSearchDimValueUrl(input.getItem().getSearchDimValueUrl());
        app.setUseHrOrg(input.getItem().getUseHrOrg());
        AppMapper.insertSelective(app);
        output.setItem(app);
        return output;
    }

    @Transactional
    public DeleteAppOutput DeleteApp(DeleteAppInput input){
        DeleteAppOutput output = new DeleteAppOutput();
        AppMapper.delete(input.getItem());
        return output;
    }

    @Transactional
    public EditAppOutput EditApp(EditAppInput input){
        EditAppOutput output=new EditAppOutput();
        int AppCodeCount=AppMapper.GetAllAppsByAppCodeNoId(input.getItem().getAppCode(),input.getItem().getId()).size();
        if (AppCodeCount > 0)
            throw new ErrorJsonResult("1000031","应用编号已存在！");
        int AppNameCount=AppMapper.GetAllAppsByAppNameNoId(input.getItem().getAppName(),input.getItem().getId()).size();
        if (AppNameCount > 0)
            throw new ErrorJsonResult("1000031","应用名称已存在！");
        Apps oldApp= AppMapper.selectByPrimaryKey(input.getItem().getId());
        oldApp.setAppCode(input.getItem().getAppCode());
        oldApp.setAppCode(input.getItem().getAppCode());
        oldApp.setAppName(input.getItem().getAppName());
        oldApp.setDescription(input.getItem().getDescription());
        oldApp.setHomeUrl(input.getItem().getHomeUrl());
        oldApp.setSearchDimValueUrl(input.getItem().getSearchDimValueUrl());
        oldApp.setUseHrOrg(input.getItem().getUseHrOrg());
        oldApp.setDescription(input.getItem().getDescription());
        AppMapper.updateByPrimaryKey(oldApp);
        return  output;
    }

    @Transactional
    public GetAppsOutput GetApps(GetAppsInput input){
        GetAppsOutput output = new GetAppsOutput();

        logger.error("log4j2 error!");

        PageHelper.startPage(input.getPage().getPageNo(),input.getPage().getPageSize(),"Id desc");
        //List<Apps> ls = AppMapper.SearchApps(input);
        List<Apps> ls=AppMapper.selectAll();
        PageInfo<Apps> page = new PageInfo<>(ls);
        output.setItems(new PagedResult<Apps>(page.getList(),(int)page.getTotal()));
        return  output;
    }

    @Transactional
    public PageLoadOutput PageLoad(PageLoadInput input){
        PageLoadOutput output = new PageLoadOutput();
        output.setItems(GetApps(input).getItems());
        return output;
    }

    @Transactional
    public GetPermissionAppOutput GetPermissionApp(GetPermissionAppInput input){
        //当前登录人的角色信息

        //根据角色信息找到所有的权限信息

        //找到管理中心的APPID

        //找到权限信息

        GetPermissionAppOutput output = new GetPermissionAppOutput();
        return  output;
    }
    //---------AppService-------------

}
